import 'dart:io';

void main() {
  List<Map<String, double>> incomeList = [];
  List<Map<String, double>> expenseList = [];

  print('Welcome to the Budget Management Application!');

  while (true) {
    print('\nPlease choose an action:');
    print('1. Add income');
    print('2. Add expense');
    print('3. View summary');
    print('4. Exit');

    stdout.write('\nEnter your choice: ');
    String? choice = stdin.readLineSync();

    if (choice == null) {
      print('Invalid input. Please try again.');
      continue;
    }

    if (choice == '4') {
      print('Exiting the program. Goodbye!');
      break;
    }

    if (choice == '1') {
      stdout.write('Enter income description: ');
      String? description = stdin.readLineSync();
      if (description == null || description.isEmpty) {
        print('Description cannot be empty.');
        continue;
      }

      double? amount;
      while (true) {
        stdout.write('Enter income amount: ');
        String? amountInput = stdin.readLineSync();
        if (amountInput == null) {
          print('Invalid input. Please enter a number.');
          continue;
        }
        amount = double.tryParse(amountInput);
        if (amount == null || amount < 0) {
          print('Amount must be a positive number.');
          continue;
        }
        break;
      }

      incomeList.add({description: amount});
      print('Income added: $description - \$${amount.toStringAsFixed(2)}');

    } else if (choice == '2') {
      stdout.write('Enter expense description: ');
      String? description = stdin.readLineSync();
      if (description == null || description.isEmpty) {
        print('Description cannot be empty.');
        continue;
      }

      double? amount;
      while (true) {
        stdout.write('Enter expense amount: ');
        String? amountInput = stdin.readLineSync();
        if (amountInput == null) {
          print('Invalid input. Please enter a number.');
          continue;
        }
        amount = double.tryParse(amountInput);
        if (amount == null || amount < 0) {
          print('Amount must be a positive number.');
          continue;
        }
        break;
      }

      expenseList.add({description: amount});
      print('Expense added: $description - \$${amount.toStringAsFixed(2)}');

    } else if (choice == '3') {
      double totalIncome = 0;
      double totalExpenses = 0;

      for (var item in incomeList) {
        totalIncome += item.values.first;
      }

      for (var item in expenseList) {
        totalExpenses += item.values.first;
      }

      double net = totalIncome - totalExpenses;

      if (net >= 0) {
        print('Your net profit is \$${net.toStringAsFixed(2)}');
        double tax = net * 0.15;
        print('Tax to be paid (15%): \$${tax.toStringAsFixed(2)}');
      } else {
        print('Your net loss is \$${(-net).toStringAsFixed(2)}');
        print('No tax is due.');
      }
    } else {
      print('Invalid choice. Please select a valid option.');
    }
  }
}
